//
//  ChapterView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/12.
//

import UIKit

class ChapterView: UIView {
        
    lazy var bookImage: UIImageView = {
        let image = UIImageView(image: UIImage(named: "placeholder_cover_ver"))
        image.contentMode = .scaleAspectFill
        image.wn_cornerWithBorder(2)
        image.clipsToBounds = true
        return image
    }()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 16)
        label.textColor = .black
        label.numberOfLines = 3
        return label
    }()
    
    lazy var countLabel: UILabel = {
        let label = UILabel()
        label.font = .wn_font(name: .bold, size: 14)
        label.textColor = .init(named: "black_333333")
        return label
    }()
        
    var model: BookDetailModel? {
        didSet {
            if let m = model {
                bookImage.wn_setupImg(m.coverUrl, placeHolder: "placeholder_cover_ver")
                nameLabel.text = m.name
                countLabel.text = "\(m.chapters)章節"
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(bookImage)
        addSubview(nameLabel)
        addSubview(countLabel)
        bookImage.snp.makeConstraints { (make) in
            make.top.equalTo(15)
            make.left.equalTo(15)
            make.size.equalTo(CGSize(width: scaleWidth(61), height: scaleWidth(90)))
        }
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(bookImage.snp.top).offset(0)
            make.left.equalTo(bookImage.snp.right).offset(10)
            make.right.equalTo(-10)
        }
        countLabel.snp.makeConstraints { (make) in
            make.top.equalTo(nameLabel.snp.bottom).offset(6)
            make.left.equalTo(nameLabel.snp.left)
            make.right.equalTo(-10)
        }
    }
    
    func darkModeConfig() {
        nameLabel.textColor = .init(named: isDark() ? "gray_666666" : "black_333333")
        countLabel.textColor = .init(named: isDark() ? "gray_666666" : "black_333333")
        backgroundColor = isDark() ? .init(named: "black_222222") : .white
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
